# -*- coding:utf-8 -*-
import os
import re
from requests import request
import requests
import timeout_decorator

path = os.path.dirname(os.path.abspath(__file__))
app_key_file = os.path.join(path, 'database/appkey.txt')

if not os.path.exists(app_key_file):
    os.mknod(app_key_file)
app_key = open(app_key_file, 'r').read()

# url = "http://192.168.10.183:6009/"
# url = "http://192.168.10.6:6009/"
url = "http://0.0.0.0:6688/"

def post_process(input):
    global app_key_file
    global app_key
    global url
    try:
        response = request(
            'post',
            url + 'algorithm/cost_name_post_processing_query',
            json=dict(
                input_list=input,
                app_key=app_key,
            )
        )
    except:
        app_key = request(
            'post',
            url + 'alg_auth/app/register',
            json=dict(
                name='test',
            )
        ).json()['data']
        with open(app_key_file, 'w') as f:
            f.write(app_key)
        response = request(
            'post',
            url + 'algorithm/cost_name_post_processing_query',
            json=dict(
                input_list=input,
                app_key=app_key,
            )
        )
    result = response.json()["data"]
    return result


def post_process1(input):
    global app_key_file
    global app_key
    global url
    try:
        response = request(
            'post',
            url + 'big_data/bill_info/post_process',
            json=dict(
                input_list=input,
                app_key=app_key,
            )
        )
    except:
        app_key = request(
            'post',
            url + 'alg_auth/app/register',
            json=dict(
                name='test',
            )
        ).json()['data']
        with open(app_key_file, 'w') as f:
            f.write(app_key)
        response = request(
            'post',
            url + 'big_data/bill_info/post_process',
            json=dict(
                input_list=input,
                app_key=app_key,
            )
        )
    result = response.json()["data"][0]["mapping_name"]

    return result

def hos_normal(hos_name):
    global app_key
    global url
    try:
        response = request(
            'post',
            url + 'big_data/hospital/normalize',
            json=dict(
                name=hos_name,
                app_key=app_key,
            )
        )
    except:
        app_key = request(
            'post',
            url + 'alg_auth/app/register',
            json=dict(
                name='test',
            )
        ).json()['data']
        with open(app_key_file, 'w') as f:
            f.write(app_key)
        response = request(
            'post',
            url + 'big_data/hospital/normalize',
            json=dict(
                name=hos_name,
                app_key=app_key,
            )
        )
    if response.json()['data']:
        result = response.json()['data'][0]
        return result
    else:
        return ""


# 药品归一化
@timeout_decorator.timeout(3)
def medicine_normalize(input):
    global app_key_file
    global app_key
    global url
    input = re.sub("[#]+|0\.9%", "", input)
    response = requests.get(url=url + 'algorithm/medicine_normalization_query', params=dict(name=input))
    # 如果自动通过返回归一化结果，否则返回原输出
    result = response.json()
    if result.get("data"):
        result = result["data"]
        if result[1] == True:
            result = result[0][0][0]
    return result

# @timeout_decorator.timeout(3)
def service_normalize(input):
    global app_key_file
    global app_key
    global url
    response = requests.get(url=url + 'algorithm/medical_service_normalization_query',
                            params=dict(name=input))
    # 如果自动通过返回归一化结果，否则返回原输出
    result = response.json()
    if result.get("data"):
        result = result["data"]
        if result[1]:
            result = result[0][0][0]
        else:
            result = ""
    return result


if __name__ == '__main__':
    # input = [{"医院名称": "",
    #          "费用名称": "带(甲)左氧氟沙星滴眼液",
    #          "number": "",
    #          "amount": "",
    #          "mapping_name": ""}]
    # print(post_process(input))
    print(service_normalize("低分子肝素测定（LMWH）"))
